"use strict";

var mongo = require('./../../../services/core/mongoservice'),
    userService = require('./../../../services/userservice'),
    shopService = require('./../../../services/shopservice'),
    alertService = require('./../../../services/alertservice'),
    alertCreationService = require('./../../../services/alertcreationservice'),
    userCreationService = require('./../../../services/usercreationservice'),
    shopCreationService = require('./../../../services/shopcreationservice'),
    referenceService = require('./../../../services/referenceservice'),
    shopCategoryService = require('./../../../services/shopcategoryservice'),
    locationCityService = require('./../../../services/locationcityservice'),
    ObjectId = require('mongodb').ObjectID,
    Q = require('q');

function Data () {
    console.log("Data constructor");
    this.inject = function () {
        console.log("Inject");
        var self = this;
        return mongo.createCollection("shops").then(function () {
            return mongo.ensureIndex("shops", { "location.loc": "2dsphere" }).then(function () {
                return mongo.createCollection("users").then(function () {
                    return mongo.ensureIndex("users", { "location.loc": "2dsphere" }).then(function () {
                        return mongo.createCollection("alerts").then(function () {
                            return mongo.ensureIndex("alerts", { "shop.location.loc": "2dsphere" }).then(function () {
                                return mongo.ensureIndex("shops", { "login": 1 }).then(function () {
                                    return mongo.ensureIndex("shops", { "users.subscribers": 1 }).then(function () {
                                        return mongo.ensureIndex("shops", { "users.near": 1 }).then(function () {
                                            return mongo.ensureIndex("shops", { "users.employees": 1 }).then(function () {
                                                return mongo.ensureIndex("shops", { "shops.subscribers": 1 }).then(function () {
                                                    return mongo.ensureIndex("shops", { "shops.near": 1 }).then(function () {
                                                        return mongo.ensureIndex("users", { "login": 1 }).then(function () {
                                                            return mongo.ensureIndex("users", { "users.subscriptions": 1 }).then(function () {
                                                                return mongo.ensureIndex("users", { "users.near": 1 }).then(function () {
                                                                    return mongo.ensureIndex("users", { "shops.subscriptions": 1 }).then(function () {
                                                                        return mongo.ensureIndex("users", { "shops.near": 1 }).then(function () {
                                                                            return mongo.ensureIndex("users", { "shops.employers": 1 }).then(function () {
                                                                                return mongo.ensureIndex("alerts", { "users.subscribers": 1 }).then(function () {
                                                                                    return mongo.ensureIndex("alerts", { "users.near": 1 }).then(function () {
                                                                                        return mongo.ensureIndex("alerts", { "shops.subscribers": 1 }).then(function () {
                                                                                            return mongo.ensureIndex("alerts", { "shops.near": 1 }).then(function () {
                                                                                                return mongo.createCollection("references").then(function () {
                                                                                                    return mongo.ensureIndex("references", { "login": 1 }).then(function () {
                                                                                                        return mongo.createCollection("references.unauthorized").then(function () {
                                                                                                            return mongo.ensureIndex("references.unauthorized", { "login": 1 }).then(function () {
                                                                                                                return mongo.createCollection("shops.categories").then(function () {
                                                                                                                    return mongo.ensureIndex("shops.categories", { "name": 1 }).then(function () {
                                                                                                                        return mongo.createCollection("location.cities").then(function () {
                                                                                                                            return mongo.ensureIndex("location.cities", { "name": 1 }).then(function () {
                                                                                                                                return mongo.createCollection("research").then(function () {
                                                                                                                                    return mongo.ensureIndex("research", { "value": 1 }).then(function () {
                                                                                                                                        return mongo.createCollection("images").then(function () {
                                                                                                                                        });
                                                                                                                                    });
                                                                                                                                });
                                                                                                                            });
                                                                                                                        });
                                                                                                                    });
                                                                                                                });
                                                                                                            });
                                                                                                        });
                                                                                                    });
                                                                                                });
                                                                                            });
                                                                                        });
                                                                                    });
                                                                                });
                                                                            });
                                                                        });
                                                                    });
                                                                });
                                                            });
                                                        });
                                                    });
                                                });
                                            });
                                        });
                                    });
                                });
                            });
                        });
                    });
                });
            });
        }).then(function () {
            var group = {
                name: "Voyage",
                categories: [
                    { 
                        name: "Agence de voyage",
                        alerts: [
                            { name: "Information" },
                            { name: "Promotion" },
                            { name: "Nouveauté" },
                            { name: "Evenement" }
                        ]
                    }
                ]
            }
            return shopCategoryService.mongoSave(group);
        }).then(function () {
            var group = {
                name: "Alimentaire",
                categories: [
                    {
                        name: "Boulangerie",
                        alerts: [
                            { name: "Information" },
                            { name: "Promotion" },
                            { name: "Nouveauté" },
                            { name: "Evenement" }
                        ]
                    },
                    {
                        name: "Boucherie",
                        alerts: [
                            { name: "Information" },
                            { name: "Promotion" },
                            { name: "Nouveauté" },
                            { name: "Evenement" }
                        ]
                    },
                    {
                        name: "Cave à vin",
                        alerts: [
                            { name: "Information" },
                            { name: "Promotion" },
                            { name: "Nouveauté" },
                            { name: "Evenement" }
                        ]
                    },
                    {
                        name: "Chocolatier",
                        alerts: [
                            { name: "Information" },
                            { name: "Promotion" },
                            { name: "Nouveauté" },
                            { name: "Evenement" }
                        ]
                    },
                    {
                        name: "Epicerie",
                        alerts: [
                            { name: "Information" },
                            { name: "Promotion" },
                            { name: "Nouveauté" },
                            { name: "Evenement" }
                        ]
                    },
                    {
                        name: "Formagerie",
                        alerts: [
                            { name: "Information" },
                            { name: "Promotion" },
                            { name: "Nouveauté" },
                            { name: "Evenement" }
                        ]
                    },
                    {
                        name: "Poissonnerie",
                        alerts: [
                            { name: "Information" },
                            { name: "Promotion" },
                            { name: "Nouveauté" },
                            { name: "Evenement" }
                        ]
                    },
                    {
                        name: "Restaurant",
                        alerts: [
                            { name: "Information" },
                            { name: "Promotion" },
                            { name: "Nouveauté" },
                            { name: "Evenement" }
                        ]
                    },
                    {
                        name: "Restauration rapide",
                        alerts: [
                            { name: "Information" },
                            { name: "Promotion" },
                            { name: "Nouveauté" },
                            { name: "Evenement" }
                        ]
                    },
                    {
                        name: "Traiteur",
                        alerts: [
                            { name: "Information" },
                            { name: "Promotion" },
                            { name: "Nouveauté" },
                            { name: "Evenement" }
                        ]
                    },
                    {
                        name: "Charcuterie",
                        alerts: [
                            { name: "Information" },
                            { name: "Promotion" },
                            { name: "Nouveauté" },
                            { name: "Evenement" }
                        ]
                    },
                    {
                        name: "Patisserie",
                        alerts: [
                            { name: "Information" },
                            { name: "Promotion" },
                            { name: "Nouveauté" },
                            { name: "Evenement" }
                        ]
                    },
                    { name: "Primeur",
                        alerts: [
                            { name: "Information" },
                            { name: "Promotion" },
                            { name: "Nouveauté" },
                            { name: "Evenement" }
                        ]
                    }]
            };
            return shopCategoryService.mongoSave(group);
        }).then(function () {
            var group = {
                name: "Habillement",
                categories: [
                    {
                        name: "Prêt à porter",
                        alerts: [
                            { name: "Information" },
                            { name: "Evenement" }
                        ]
                    }
                ]
            };
            return shopCategoryService.mongoSave(group);
        }).then(function () {
            var group = {
                name: "Municipalité",
                categories: [
                    {
                        name: "Municipalité",
                        alerts: [
                            { name: "Information" },
                            { name: "Evenement" }
                        ]
                    }
                ]
            };
            return shopCategoryService.mongoSave(group);
        }).then(function () {

            var city = {
                name: "Viroflay",
                //location: 
                neighborhoods: [
                    { name: "Les Aulnettes" }, //ajouter location
                    { name: "Rive Droite" },
                    { name: "La Ville au Bois" },
                    { name: "Les Arcades" },
                    { name: "Le Haras" },
                    { name: "Rive Gauche" },
                    { name: "Verdun" },
                    { name: "Foret"}]
            }
            return locationCityService.mongoSave(city);

        }).then(function () {
            return referenceService.mongoUnauthorizedLogin("JMB");
        }).then(function () {
            return userCreationService.getUserModel().then(function (u) {
                u.login = "pat";
                u.password = "pat";
                u.contact.email = "pat@exemple.com";
                u.location.address = "65 Rue Yves le Coz, 78000 Versailles, France";
                u.location.route = "Rue Yves le Coz";
                u.location.city = "Versailles";
                u.location.postal_code = "78000";
                u.location.loc.coordinates[0] = 2.1579446;
                u.location.loc.coordinates[1] = 48.7958946;

                return userService.mongoInsert(u);
            });
        }).then(function () {
            return userCreationService.getUserModel().then(function (u) {
                u.login = "ben";
                u.password = "ben";
                u.contact.email = "ben@exemple.com";
                u.location.address = "30 Rue Yves le Coz, 78000 Versailles, France";
                u.location.route = "Rue Yves le Coz";
                u.location.city = "Versailles";
                u.location.postal_code = "78000";
                u.location.loc.coordinates[0] = 2.1579946;
                u.location.loc.coordinates[1] = 48.7959946;

                return userService.mongoInsert(u);
            });
        }).then(function () {
            return shopCreationService.getShopModel().then(function (s) {
                return shopCategoryService.mongoGetByGroup("Habillement").then(function (group) {
                    expect(group.categories[0].name).toEqual("Prêt à porter");

                    s.login = "vêti";
                    s.name = "Vêti";
                    s.categories.push(group.categories[0]);
                    s.description = "Spécialiste du prêt à porte. Vente de #vêtements"
                    s.location.address = "40 Rue Yves le Coz, 78000 Versailles, France";
                    s.location.route = "Rue Yves le Coz";
                    s.location.city = "Versailles";
                    s.location.postal_code = "78000";
                    s.location.loc.coordinates[0] = 2.1585988;
                    s.location.loc.coordinates[1] = 48.7967948;

                    return shopService.mongoInsert(s).then(function (shop) {
                        return userService.mongoGetPageByLogin("pat").then(function (user) {
                            return shopService.mongoAddEmployee(shop._id, user._id);
                        });
                    });
                });
            });
        }).then(function () {
            return shopCreationService.getShopModel().then(function (s) {
                return shopCategoryService.mongoGetByGroup("Alimentaire").then(function (group) {
                    expect(group.categories[0].name).toEqual("Boulangerie");
                    expect(group.categories[11].name).toEqual("Patisserie");

                    s.login = "LeFournilDuRoi";
                    s.name = "Le Fournil du Roi";
                    s.categories.push(group.categories[0]);
                    s.categories.push(group.categories[11]);
                    s.description = "Boulangerie  #artisanale"
                    s.contact.tel = "0600000001";
                    s.contact.email = "lefournilduroi@boulangerie.fr";
                    s.location.address = "45 Rue Yves le Coz, 78000 Versailles, France";
                    s.location.route = "Rue Yves le Coz";
                    s.location.city = "Versailles";
                    s.location.postal_code = "78000";
                    s.location.loc.coordinates[0] = 2.1585288;
                    s.location.loc.coordinates[1] = 48.7907958;

                    return shopService.mongoInsert(s).then(function (shop) {
                        return userService.mongoGetPageByLogin("ben").then(function (user) {
                            return shopService.mongoAddEmployee(shop._id, user._id);
                        });
                    });
                });
            });
        }).then(function () {
            return userCreationService.getUserModel().then(function (u) {
                u.login = "paris";
                u.password = "paris";
                u.contact.email = "paris@exemple.com";
                u.location.address = "32 Avenue Pierre Mendès-France, 75013 Paris, France";
                u.location.route = "Avenue Pierre Mendès-France";
                u.location.city = "Paris";
                u.location.postal_code = "75013";
                u.location.loc.coordinates[0] = 2.370225;
                u.location.loc.coordinates[1] = 48.838758;

                return userService.mongoInsert(u).then(function (user) {
                    return shopService.mongoGetPageByGps(2.1585988, 48.7967948).then(function (shop) { //shop 1
                        return shopService.mongoAddUserSubscriber(shop._id, user._id);
                    });
                });
            });
        }).then(function () {
            var promises = [];
            var index = 1;
            for (var i = 0; i < 2; i++) {
                var p = shopService.mongoGetPageByGps(2.1585988, 48.7967948).then(function (shop) {
                    return alertCreationService.getAlertModel().then(function (alert) {
                        alert.type = { name: "promotion", code: "promo" };
                        alert.articles[0].content = "alerte numéro " + (index++);
                        alert.dates.delay = "3h";

                        return alertService.mongoInsert(shop._id, alert);
                    });
                });
                promises.push(p);
            }
            return Q.all(promises);
        }).then(function () {
            return userCreationService.getUserModel().then(function (u) {
                u.login = "benji";
                u.password = "benji";
                u.contact.email = "benji@exemple.com";
                u.location.address = "55 Rue Yves le Coz, 78000 Versailles, France";
                u.location.city = "Paris";
                u.location.postal_code = "75013";
                u.location.loc.coordinates[0] = 2.1579446;
                u.location.loc.coordinates[1] = 48.7957346;

                return userService.mongoInsert(u).then(function (user) {
                    return shopService.mongoGetPageByGps(2.1585988, 48.7967948).then(function (shop) { //shop1
                        return shopService.mongoAddUserSubscriber(shop._id, user._id);
                    });
                });
            });
        }).then(function () {
            return alertCreationService.getAlertModel().then(function (alert) {
                alert.type = { name: "événement", code: "event" };
                alert.articles[0].content = "alerte numéro 3";
                alert.dates.delay = "1d";
                alert.tags.push("événement");
                return shopService.mongoGetPageByGps(2.1585988, 48.7967948).then(function (shop) {
                    return alertService.mongoInsert(shop._id, alert);
                });
            });
        }).then(function () {
            return alertCreationService.getAlertModel().then(function (alert) {
                alert.type = { name: "événement", code: "event" };
                alert.articles[0].content = "alerte numéro 4. Ne doit pas s'afficher car trop vieille.";
                alert.dates.delay = "1h";
                alert.tags.push("information");

                var now = new Date();
                now.setDate(now.getDate() - 1); //-1 day
                alert.dates.creation = now;

                return shopService.mongoGetPageByGps(2.1585988, 48.7967948).then(function (shop) { //shop 1
                    return alertService.mongoInsert(shop._id, alert);
                });
            });
        }).then(function () {
            return userService.mongoGetPageByLogin("ben").then(function (user) {
                return shopService.mongoGetPageByGps(2.1585288, 48.7907958).then(function (shop) { //shop 2
                    return shopService.mongoAddEmployee(shop._id, user._id);
                });
            });
        }).then(function () {
            return shopService.mongoGetPageByGps(2.1585988, 48.7967948).then(function (shop1) { //shop 1
                return shopService.mongoGetPageByGps(2.1585288, 48.7907958).then(function (shop2) { //shop 2
                    return shopService.mongoAddShopSubscriber(shop1._id, shop2._id);
                });
            });
        }).then(function () {
            console.log("-------------------------------------------------");
            console.log("data injection completed");
            console.log("-------------------------------------------------");
        }).fail(function (e) {
            console.log("-------------------------------------------------");
            console.log("Error: " + e.message + " " + e.stack);
            console.log("-------------------------------------------------");
        });
    },

    this.clear = function () {
        var self = this;

        return mongo.remove("alerts.reports.archives").then(function () {
            return mongo.remove("alerts.archives");
        }).then(function () {
            return mongo.remove("alerts.reports");
        }).then(function () {
            return mongo.remove("alerts");
        }).then(function () {
            return mongo.remove("shops.reports.archives");
        }).then(function () {
            return mongo.remove("shops.archives");
        }).then(function () {
            return mongo.remove("shops.reports");
        }).then(function () {
            return mongo.remove("shops");
        }).then(function () {
            return mongo.remove("shops.categories");
        }).then(function () {
            return mongo.remove("users.creation");
        }).then(function () {
            return mongo.remove("users.reports.archives");
        }).then(function () {
            return mongo.remove("users.archives");
        }).then(function () {
            return mongo.remove("users.reports");
        }).then(function () {
            return mongo.remove("users");
        }).then(function () {
            return mongo.remove("research");
        }).then(function () {
            return mongo.remove("references");
        }).then(function () {
            return mongo.remove("references.unauthorized");
        }).then(function () {
            return mongo.remove("location.cities");
        }).then(function () {
            return mongo.remove("tags");
        });


    }
};

exports = module.exports = new Data();